package top.aicey.ai.sparring.dto.model;

import lombok.Data;
import top.aicey.ai.sparring.common.constants.SuccessConstants;
import top.aicey.ai.sparring.common.exception.BaseException;
import top.aicey.ai.sparring.common.holder.RequestHeaderHolder;
import top.aicey.ai.sparring.common.utils.UUIDUtils;
import org.springframework.util.StringUtils ;

import java.io.Serializable;

@Data
public class BaseResponse<T>  implements Serializable {
    private static final long serialVersionUID = -8244372110143879374L;
    private String request_id;
    private String success = "true";
    private String code;
    private String message;
    private T data;

    public BaseResponse() {
    }

    public static BaseResponse getSuccessResponse() {
        return baseResponse(true, SuccessConstants.SUCCESS.getCode(), SuccessConstants.SUCCESS.getMessage(), (Object)null);
    }

    public static <T> BaseResponse<T> getSuccessResponse(T data) {
        return baseResponse(true, SuccessConstants.SUCCESS.getCode(), SuccessConstants.SUCCESS.getMessage(), data);
    }

    public static <T> BaseResponse<T> getSuccessResponse(SuccessContext successContext, T data) {
        return baseResponse(true, successContext.getCode(), successContext.getMessage(), data);
    }

    public static BaseResponse getFailedResponse(BaseException exception) {
        return baseResponse(false, exception.getError().getCode(), exception.getError().getMessage(), exception.getData());
    }

    public static BaseResponse getFailedResponse(ErrorContext errorContext) {
        return baseResponse(false, errorContext.getCode(), errorContext.getMessage(), (Object)null);
    }

    public static <T> BaseResponse<T> getFailedResponse(ErrorContext errorContext, T data) {
        return baseResponse(false, errorContext.getCode(), errorContext.getMessage(), data);
    }

    private static <T> BaseResponse<T> baseResponse(Boolean success, String code, String message, T data) {
        BaseResponse result = new BaseResponse();
        result.setRequest_id(RequestHeaderHolder.getRequestHeader().getRequestId());
        if (StringUtils.isEmpty(result.getRequest_id())) {
            result.setRequest_id(UUIDUtils.get32UUID());
        }

        result.setSuccess(String.valueOf(success));
        result.setCode(code);
        result.setMessage(message);
        result.setData(data);
        return result;
    }

}
